﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Entity;
using LotteryVote.Utility;
using System.Web;
using System.Web.Caching;
using System.Configuration;

namespace LotteryVote.Logic.Application
{
    public class LotterySystem
    {
        #region Const Strings
        private const string M_GAMEPLAYWAY = "CacheGamePlayWay";
        private const string M_DEFAULTUPPERLIMIT = "DefaultUpperLimit";
        private const string M_ADDITIONALLASTBETMINUTES = "AdditionalLastBetMinutes";
        private const string M_AUTOELECTIONCODECOMMISSION = "AutoElectionCodeCommission";
        private const string M_TWELVEZODIAC = "TwelveZodiac";
        private const string M_EVENODD = "EvenOdd";
        private const string M_ODDEVEN = "OddEven";
        private const string M_EVENEVEN = "EVENEVEN";
        private const string M_ODDODD = "OddOdd";
        private const string M_ALLCOMPANYLIST = "AllCompanylist";
        #endregion

        #region Fields
        #endregion

        private static object _lockHelper = new object();
        private static LotterySystem _system;
        private LotterySystem()
        {
            _gpwDic = new Dictionary<int, GamePlayWay>();
            _mixKeyGPWDic = new Dictionary<string, GamePlayWay>();
        }

        private IDictionary<int, GamePlayWay> _gpwDic;
        private IDictionary<string, GamePlayWay> _mixKeyGPWDic;

        private GamePlayWayFacade _blGPW;
        public GamePlayWayFacade BlGamePlayWay
        {
            get
            {
                if (_blGPW == null)
                    _blGPW = new GamePlayWayFacade();
                return _blGPW;
            }
            set
            {
                _blGPW = value;
            }
        }
        private CompanyFacade _blCompany;
        public CompanyFacade BlCompany
        {
            get
            {
                if (_blCompany == null)
                    _blCompany = new CompanyFacade();
                return _blCompany;
            }
            set
            {
                _blCompany = value;
            }
        }

        public static LotterySystem Current
        {
            get
            {
                if (_system == null)
                {
                    lock (_lockHelper)
                    {
                        if (_system == null)
                            _system = new LotterySystem();
                    }
                }
                return _system;
            }
        }

        /// <summary>
        ///分页每页呈现元素数量.
        /// </summary>
        public int PageItemCount
        {
            get
            {
                return 20;
            }
        }
        public IList<GamePlayWay> GamePlayWay
        {
            get
            {
                var gamplaywayList = HttpContext.Current.Cache[M_GAMEPLAYWAY] as IList<GamePlayWay>;
                if (gamplaywayList == null)
                {
                    gamplaywayList = BlGamePlayWay.GetAllGamePlayWay();
                    HttpContext.Current.Cache.Add(M_GAMEPLAYWAY, gamplaywayList, null, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(4), CacheItemPriority.Default, null);
                }
                return gamplaywayList;
            }
        }
        public IList<LotteryCompany> AllCompanyList
        {
            get
            {
                var companyList = HttpContext.Current.Cache[M_ALLCOMPANYLIST] as IList<LotteryCompany>;
                if (companyList == null || companyList.Count == 0)
                {
                    companyList = BlCompany.GetAllCompany();
                    HttpContext.Current.Cache.Add(M_ALLCOMPANYLIST, companyList, null, Cache.NoAbsoluteExpiration, TimeSpan.FromDays(1), CacheItemPriority.Default, null);
                }
                return companyList;
            }
        }
        /// <summary>
        /// 投注上限默认金额.
        /// </summary>
        public decimal DefaultUpperLimit
        {
            get
            {
                string limitStr = ConfigurationManager.AppSettings[M_DEFAULTUPPERLIMIT];
                decimal limitValue;

                return decimal.TryParse(limitStr, out limitValue) ? limitValue : 10000000000;
            }
        }
        /// <summary>
        /// 走地时间，关盘之后，尾还能投注的时间(默认5分钟).
        /// </summary>
        public double AdditionalLastBetMinutes
        {
            get
            {
                string minuteStr = ConfigurationManager.AppSettings[M_ADDITIONALLASTBETMINUTES];
                double minute;
                return double.TryParse(minuteStr, out minute) ? minute : 5;
            }
        }
        /// <summary>
        ///自动选码佣金(默认0.5)，用于十二生肖，双单，双双，单双，单单
        /// </summary>
        public double AutoElectionCodeCommission
        {
            get
            {
                string commStr = ConfigurationManager.AppSettings[M_AUTOELECTIONCODECOMMISSION];
                double comm;
                return double.TryParse(commStr, out comm) ? comm : 0.5;
            }
        }
        /// <summary>
        /// 十二生肖号码.
        /// </summary>
        public int[] TwelveZodiac
        {
            get
            {
                string zodiacStr = ConfigurationManager.AppSettings[M_TWELVEZODIAC];
                int[] zodiacArr;
                if (string.IsNullOrEmpty(zodiacStr))
                    zodiacArr = new[] { 
                        06, 07, 09, 10, 11, 12, 14, 15,
                        18, 46, 47, 49, 50, 51, 52, 54,
                        55, 58, 86, 87, 89, 90, 91, 92,
                        94, 95, 98, 23, 26, 28, 32, 35,
                        63, 66, 68, 72, 75
                    };
                else
                {
                    var strArr = zodiacStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    zodiacArr = new int[strArr.Length];
                    for (int i = 0; i < strArr.Length; i++)
                        zodiacArr[i] = int.Parse(strArr[i]);
                }
                return zodiacArr;
            }
        }
        /// <summary>
        /// 双单号码.
        /// </summary>
        public int[] EvenOdd
        {
            get
            {
                string appSettingStr = ConfigurationManager.AppSettings[M_EVENODD];
                int[] returnVal;
                if (string.IsNullOrEmpty(appSettingStr))
                    returnVal = new[] { 
                        01, 21, 41, 61, 81, 
                        03, 23, 43, 63, 83, 
                        05, 25, 45, 65, 85,
                        07, 27, 47, 67, 87, 
                        09, 29, 49, 69, 89 
                    };
                else
                {
                    var strArr = appSettingStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    returnVal = new int[strArr.Length];
                    for (int i = 0; i < strArr.Length; i++)
                        returnVal[i] = int.Parse(strArr[i]);
                }
                return returnVal;
            }
        }
        /// <summary>
        /// 单双号码.
        /// </summary>
        public int[] OddEven
        {
            get
            {
                string appSettingStr = ConfigurationManager.AppSettings[M_ODDEVEN];
                int[] returnVal;
                if (string.IsNullOrEmpty(appSettingStr))
                    returnVal = new[] { 
                        10,30,50,70,90,
                        12,32,52,72,92,
                        14,34,54,74,94,
                        16,36,56,76,96,
                        18,38,58,78,98
                    };
                else
                {
                    var strArr = appSettingStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    returnVal = new int[strArr.Length];
                    for (int i = 0; i < strArr.Length; i++)
                        returnVal[i] = int.Parse(strArr[i]);
                }
                return returnVal;
            }
        }
        /// <summary>
        /// 双双号码.
        /// </summary>
        public int[] EvenEven
        {
            get
            {
                string appSettingStr = ConfigurationManager.AppSettings[M_EVENEVEN];
                int[] returnVal;
                if (string.IsNullOrEmpty(appSettingStr))
                    returnVal = new[] { 
                        00,20,40,60,80,
                        02,22,42,62,82,
                        04,24,44,64,84,
                        06,26,46,66,86,
                        08,28,48,68,88
                    };
                else
                {
                    var strArr = appSettingStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    returnVal = new int[strArr.Length];
                    for (int i = 0; i < strArr.Length; i++)
                        returnVal[i] = int.Parse(strArr[i]);
                }
                return returnVal;
            }
        }
        /// <summary>
        /// 单单号码.
        /// </summary>
        public int[] OddOdd
        {
            get
            {
                string appSettingStr = ConfigurationManager.AppSettings[M_ODDODD];
                int[] returnVal;
                if (string.IsNullOrEmpty(appSettingStr))
                    returnVal = new[] { 
                        11,31,51,71,91,
                        13,33,53,73,93,
                        15,35,55,75,95,
                        17,37,57,77,97,
                        19,39,59,79,99
                    };
                else
                {
                    var strArr = appSettingStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    returnVal = new int[strArr.Length];
                    for (int i = 0; i < strArr.Length; i++)
                        returnVal[i] = int.Parse(strArr[i]);
                }
                return returnVal;
            }
        }

        public GamePlayWay FindGamePlayWay(int gpwId)
        {
            if (!_gpwDic.ContainsKey(gpwId))
            {
                var gpw = GamePlayWay.Find(it => it.Id == gpwId);
                _gpwDic.Add(gpwId, gpw);
            }
            return _gpwDic[gpwId];
        }
        public GamePlayWay FindGamePlayWay(int gameId, int wayId)
        {
            string key = string.Format("{0}_{1}", gameId, wayId);
            if (!_mixKeyGPWDic.ContainsKey(key))
            {
                var gpw = GamePlayWay.Find(it => it.GameType.GameId == gameId && it.PlayWay.WayId == wayId);
                _mixKeyGPWDic.Add(key, gpw);
            }
            return _mixKeyGPWDic[key];
        }
    }
}
